package org.lmib.abt.checkers;
/*
 * Copyright (c) 1996/1997 F. Di Fabio
 * All rights reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.
 */


import java.util.Vector;
import java.lang.Integer;


class Move {
  static boolean NOJUMP = false;  
  static boolean JUMP = true;
  static Move NOMOVE = null;
  static int MANSTEP = 1;
  static int KINGSTEP = 4;
  static int MANJUMP = 2;
  static int KINGJUMP = 8;
  static int PRIORITY = 100000000;
  boolean isJump;
  int direction;
  int priority;
  Box from;
  Box to;
  Move toNextJump;

public int setPriority() {
	
    priority = 0;
    if(from.getPiece() > 3) {
		int priorityLevel = 1;
		Move cur = this;
		priority += 1;
		while(cur != NOMOVE) {
		    if(cur.from.near[cur.direction].getPiece() > 3) {
		    	priority += PRIORITY/priorityLevel;
		    }
		    priorityLevel *= 10;
		    cur = cur.toNextJump;
		}
    }
    return priority;
}

public Move() {

    isJump = NOJUMP;
    to = Box.NOBOX;
    toNextJump = NOMOVE;
}

public Move(boolean j, int p, int d, Box f, Box t) {

    isJump = j;
    priority = p;
    direction = d;
    from = f;	
    to = t;
    toNextJump = NOMOVE;
}

public Move(boolean j, int p, Box f, Box t) {

    isJump = j;
    priority = p;
    from = f;
    to = t;
    toNextJump = NOMOVE;
}

public Move copy() {
  Move m = this;
  Move newMove;
  Move n;
  
    newMove = new Move(m.isJump,m.priority,m.direction,m.from,m.to);
    n = newMove;
    while(m.toNextJump != NOMOVE) {
    	m = m.toNextJump;
    	n.toNextJump = new Move(m.isJump,m.priority,m.direction,m.from,m.to);
    	n = n.toNextJump;
    }
    return newMove;
}

public void addJump(Move move) {
  Move m = this;
  
  priority = priority * 8 + move.priority;
  while(m.toNextJump != NOMOVE) { 
    m = m.toNextJump;
  }
  m.toNextJump = move;
}

public boolean isValid(Vector list) {
  int i;
  Move thisMove = this;
  Move m;
  boolean goodMove;
  
    for(i=0;i<list.size();i++) {
	    m = (Move)list.elementAt(i);
		goodMove = true;
		while(goodMove && (m != NOMOVE)) {
		    if((m.from.idBox != thisMove.from.idBox) || (m.to.idBox != thisMove.to.idBox)) {
		    	goodMove = false;
		    }
		    else {
		    	m = m.toNextJump;
		    	thisMove = thisMove.toNextJump;
		    }
		}			
		if(goodMove) {
		    m = (Move)list.elementAt(i);
		    isJump = m.isJump;
		    direction = m.direction;
		    toNextJump = m.toNextJump;
		    return true;
		}
    }
    return false;
}

public String toString() {
  StringBuffer strMove = new StringBuffer();
  Move m = this;
  
    //strMove.append(m.priority+"|");
    if(m.to != Box.NOBOX) {
    	strMove.append((m.from.idBox+1)+" -> "+(m.to.idBox+1));
    }
    while(m.toNextJump != NOMOVE) {
    	m = m.toNextJump;
    	if(m.to != Box.NOBOX) {
    		strMove.append(" -> "+(m.to.idBox+1));
    	}
    }
    return strMove.toString();
}

public Vector toVector() {
  Vector v = new Vector();
  Move m = this;
  
    if(m.to != Box.NOBOX) {
    	v.addElement( new Integer(m.from.idBox));
    	v.addElement( new Integer(m.to.idBox));
    }
    while(m.toNextJump != NOMOVE) {
    	m = m.toNextJump;
    	if(m.to != Box.NOBOX) {
    		v.addElement(new Integer(m.to.idBox));
    	}
    }
    return (Vector)v.clone();
}
}
